<template>
  <div class="main">
    <a-form
      id="formRegister"
      class="user-layout-register"
      ref="formRegister"
      :form="form"
      @submit="handleSubmit"
    >
      <a-tabs
        :activeKey="customActiveKey"
        :tabBarStyle="{ textAlign: 'center', borderBottom: 'unset' }"
        @change="handleTabClick"
      >
        <a-tab-pane key="tab1" tab="团队执教模式">
            <a-form-item>
            <a-input
              size="large"
              type="text"
              placeholder="姓名"
              v-decorator="['name', {rules: [{ required: true, message: '请输入姓名' }], validateTrigger: 'blur'}]"
            >
              <a-icon slot="prefix" type="user" :style="{ color: 'rgba(0,0,0,.25)' }" />
            </a-input>
          </a-form-item>
              <a-form-item>
            <a-input
              size="large"
              type="text"
              placeholder="单位"
              v-decorator="['team', {rules: [{ required: true, message: '请输入单位' }], validateTrigger: 'blur'}]"
            >
              <a-icon slot="prefix" type="team" :style="{ color: 'rgba(0,0,0,.25)' }" />
            </a-input>
              </a-form-item>
          <a-form-item>
            <a-input
              size="large"
              type="text"
              placeholder="手机号"
              v-decorator="['mobile', {rules: [{ required: true, pattern: /^1[34578]\d{9}$/, message: '请输入正确的手机号' }], validateTrigger: 'change'}]"
            >
              <a-icon slot="prefix" type="mobile" :style="{ color: 'rgba(0,0,0,.25)' }" />
            </a-input>
          </a-form-item>

          <a-row :gutter="16">
            <a-col class="gutter-row" :span="16">
              <a-form-item>
                <a-input
                  size="large"
                  type="text"
                  placeholder="验证码"
                  v-decorator="['captcha', {rules: [{ required: true, message: '请输入验证码' }], validateTrigger: 'blur'}]"
                >
                  <a-icon slot="prefix" type="mail" :style="{ color: 'rgba(0,0,0,.25)' }" />
                </a-input>
              </a-form-item>
            </a-col>
            <a-col class="gutter-row" :span="8">
              <a-button
                class="getCaptcha"
                tabindex="-1"
                :disabled="state.smsSendBtn"
                @click.stop.prevent="getCaptcha"
                v-text="!state.smsSendBtn && '获取验证码' || ('重新发送'+state.time+' s')"
              ></a-button>
            </a-col>
          </a-row>
        </a-tab-pane>
        <a-tab-pane key="tab2" tab="个人使用模式">
            <a-form-item>
            <a-input
              size="large"
              type="text"
              placeholder="姓名"
              v-decorator="['name', {rules: [{ required: true, message: '请输入姓名' }], validateTrigger: 'blur'}]"
            >
              <a-icon slot="prefix" type="user" :style="{ color: 'rgba(0,0,0,.25)' }" />
            </a-input>
          </a-form-item>
          <a-form-item>
            <a-input
              size="large"
              type="text"
              placeholder="手机号"
              v-decorator="['mobile', {rules: [{ required: true, pattern: /^1[34578]\d{9}$/, message: '请输入正确的手机号' }], validateTrigger: 'change'}]"
            >
              <a-icon slot="prefix" type="mobile" :style="{ color: 'rgba(0,0,0,.25)' }" />
            </a-input>
          </a-form-item>

          <a-row :gutter="16">
            <a-col class="gutter-row" :span="16">
              <a-form-item>
                <a-input
                  size="large"
                  type="text"
                  placeholder="验证码"
                  v-decorator="['captcha', {rules: [{ required: true, message: '请输入验证码' }], validateTrigger: 'blur'}]"
                >
                  <a-icon slot="prefix" type="mail" :style="{ color: 'rgba(0,0,0,.25)' }" />
                </a-input>
              </a-form-item>
            </a-col>
            <a-col class="gutter-row" :span="8">
              <a-button
                class="getCaptcha"
                tabindex="-1"
                :disabled="state.smsSendBtn"
                @click.stop.prevent="getCaptcha"
                v-text="!state.smsSendBtn && '获取验证码' || ('重新发送'+state.time+' s')"
              ></a-button>
            </a-col>
          </a-row>
        </a-tab-pane>
      </a-tabs>
            <a-form-item>
        <a-button
          size="large"
          type="primary"
          htmlType="submit"
          class="register-button"
          :loading="registerBtn"
          @click.stop.prevent="handleSubmit"
          :disabled="registerBtn">注册
        </a-button>
        <router-link class="login" :to="{ name: 'login' }">使用已有账户登录</router-link>
      </a-form-item>

    </a-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      customActiveKey: "tab1",
      form: this.$form.createForm(this),
      state: {
        time: 60,
        smsSendBtn: false,
        passwordLevel: 0,
        passwordLevelChecked: false,
        percent: 10,
        progressColor: '#FF0000'
      },
      registerBtn: false
    };
  },
  methods: {
    getCaptcha(e) {
      e.preventDefault();
      const { form: { validateFields }, state } = this

      validateFields(['mobile'], { force: true }, (err, values) => {
        if (!err) {
          state.smsSendBtn = true

          const interval = window.setInterval(() => {
            if (state.time-- <= 0) {
              state.time = 60
              state.smsSendBtn = false
              window.clearInterval(interval)
            }
          }, 1000)

          const hide = this.$message.loading('验证码发送中..', 0)
          this.$api.user.sendmess({ tel: values.mobile }).then(res => {
            setTimeout(hide, 2500)
          }).catch(err => {
            setTimeout(hide, 1)
            clearInterval(interval)
            state.time = 60
            state.smsSendBtn = false
            // this.requestFailed(err)
          })
        }
      })
    },
    handleTabClick(key) {
      this.customActiveKey = key;
      // this.form.resetFields()
    },
    handleSubmit(e) {
      e.preventDefault();
      this.form.validateFields((err, values) => {
        if (!err) {
          this.$api.user.register({ tel: values.mobile,code:values.captcha,adminname:values.name,classname:values.team }).then((res)=>{
            if(res.data.code!=1000)
            this.requestFailed(res)
            else
            this.$notification['success']({
                message: '提示',
                description: "注册成功",
                duration: 8
            })
          }).catch(err => {
              this.requestFailed(err)
            })
        }
      });
    },
    requestFailed (err) {
      this.$notification['error']({
        message: '错误',
        description: (err.data).msg || '请求出现错误，请稍后再试',
        duration: 4
      })
      this.registerBtn = false
    }
  }
};
</script>
<style lang="scss" scoped>
.user-layout-register {
  background: white;
  padding: 20px;
  label {
    font-size: 14px;
  }

  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }

.register-button {
      width: 50%;
    }
.login {
      float: right;
      line-height: 40px;
    }
}
</style>